using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core;
using Core.Graphics;
using System.IO;
using Core.Mapping;

namespace QuickXMLEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadFile();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SerializationUtilities.Utilities<TileSet> uti = new SerializationUtilities.Utilities<TileSet>();

            MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(richTextBox1.Text));

            string strFile = openFileDialog1.FileName;

            uti.SevenZipCompressionStream(ms, File.OpenWrite(strFile));
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            LoadFile();
        }

        protected void LoadFile()
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MemoryStream ms = new MemoryStream();

                //based on file extension, do different serialization
                string ext = Path.GetExtension(openFileDialog1.FileName);
                switch (ext)
                {
                    case ".TileSet":
                        SerializationUtilities.Utilities<TileSet> utiTileSet = new SerializationUtilities.Utilities<TileSet>();

                        utiTileSet.SevenZipDeCompressionStream(openFileDialog1.OpenFile(), ms);
                        break;
                    case ".TileMap":
                        SerializationUtilities.Utilities<Map> utiTileMap = new SerializationUtilities.Utilities<Map>();

                        utiTileMap.SevenZipDeCompressionStream(openFileDialog1.OpenFile(), ms);
                        break;
                    case ".tw":
                        SerializationUtilities.Utilities<TileWorld> utiTileWorld = new SerializationUtilities.Utilities<TileWorld>();

                        utiTileWorld.SevenZipDeCompressionStream(openFileDialog1.OpenFile(), ms);
                        break;
                    case ".AutoTile":
                        SerializationUtilities.Utilities<AutoTileInfo> utiAutoTile = new SerializationUtilities.Utilities<AutoTileInfo>();

                        utiAutoTile.SevenZipDeCompressionStream(openFileDialog1.OpenFile(), ms);
                        break;
                }

                //Read deserialized data into text box
                StreamReader sr = new StreamReader(ms);

                string str = sr.ReadToEnd();

                richTextBox1.Text = str;
            }
        }
    }
}